package org.groovymud.shell.security.loginsteps;

import java.io.IOException;
import java.util.UUID;

import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;

import org.groovymud.shell.security.MudLoginModuleData;
import org.groovymud.shell.security.MudPrincipal;

import static org.groovymud.shell.security.MudLoginModuleData.GUEST_USER;
import static org.groovymud.shell.security.MudLoginModuleData.PLAYER_PRINCIPAL;

public class CheckForGuest extends LoginStep {

    @Override
	public void execute(MudLoginModuleData data) throws IOException, UnsupportedCallbackException, FailedLoginException {
		if (data.getUsername().equals(GUEST_USER)) {
			data.setUsername(GUEST_USER + UUID.randomUUID().toString().substring(0, 5));
			data.setGuest(true);
			
		}else{
            data.getPlayerPrincipals().add(new MudPrincipal(PLAYER_PRINCIPAL));
		}
		if (getNextStep() != null) {
            getNextStep().execute(data);
		}
	}

}
